//获取应用实例 
const app = getApp();

Page({
	data: {
    userimg:'/static/images/avatar-p3.png"',
    nickname:'修改',
    avatar: '',
    version_android: '',
    array: ['男', '女', '保密'],
    index:1
  },
	onLoad: function(option) {
    console.log(option.avatar)
    if (option.avatar != undefined) {
      this.setData({
        userimg: option.avatar
      })
      this.upload_file(option.avatar)
    }
  },

  onShow: function (option){
    this.getUserMsg();
   
  },

  nickInput:function(e){
    this.setData({
      nicks: e.detail.value
    })
  },
  nameInput: function (e) {
    this.setData({
      names: e.detail.value
    })
    console.log(this.data.names)
  },
  sexInput: function (e) {
    this.setData({
      sexs: e.detail.value
    })
    
  },
  birthInput: function (e) {
    this.setData({
      births: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    console.log(this.data.index)
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    console.log(this.data.date)
  },

  //编辑用户信息
  savePersonal:function(){
    var that = this;
    console.log(that.data.names)
    that.setData({
      index:index+1
    })
    console.log(that.data.index)
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=editUserMsg',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        token: '' + app.globalData.token,
        truename: that.data.names,
        nickname:that.data.nicks,
        sex: that.data.index,
        birthday: that.data.date
      },
      success: (res) => {
        console.log(res)
        if(res.data.code==0){
          wx/wx.showToast({
            title: ''+res.data.msg,
            icon: 'success',
          })
        }else{
          wx.showToast({
            title: ''+res.data.msg,
            icon:'none'
          })
        }
      },
    });
  },

  // 点击上传图片
  upShopLogo: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImageShop('album');//从相册中选择
          } else if (res.tapIndex == 1) {
            that.chooseWxImageShop('camera');//手机拍照
          }
        }
      }
    })
  },

//选择图片
chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res)
        if (res.tempFiles[0].size > 1048576 * 2) {
          wx.showModal({
            title: "提示",
            content: "选择的图片过大，请上传不超过2M的图片",
            showCancel: false,
            success(res) {
              if (res.confirm) {
              }
            }
          });
          return;
        }
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          userimg: res.tempFilePaths[0]
        })
        wx.redirectTo({
          url: '/pages/cropper/cropper?src=' + res.tempFilePaths[0],
        })
      }
    })

  },

  //上传图片到服务器

upload_file: function (filePath) {
  wx.showLoading({
    title: '上传中',
  })
    var that = this;
    console.log(filePath)
    wx.uploadFile({
      url: app.globalData.api + '/app.php?c=User&a=editUserAvatar',
      filePath: filePath,
      name: 'user_avatar',
      formData: {//需要的参数
        'token': app.globalData.token,
        'user_avatar': 'filePath',
      }, // HTTP 请求中其他额外的 form data
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function (res) {
        console.log(res)
        var data = JSON.parse(res.data);
        // // var data=res.data;
        // console.log(data);
        // that.getUserMsg();
        if (data.code==0){
          wx.showToast({
            title: '上传成功',
            icon: 'success'
          })
        }
      },
      fail: function (res) {
          console.log(res)
          wx.showToast({
            title: '上传失败',
            icon:'none'
          })
      },complete(res){
        wx.hideLoading();
      }

    })

  },

  //获取用户信息
  getUserMsg() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=getUserMsg',
      data: {
        token: app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            user_msg: res.data.data.user_detail,
            nicks:res.data.data.user_detail.nickname,
            names: res.data.data.user_detail.truename,
            index: res.data.data.user_detail.sex,
            date: res.data.data.user_detail.birthday,
            userimg:res.data.data.user_detail.avatar
          })
        } else {
          if (res.data.code == 105) {
            wx.removeStorage({
              key: 'token',
              success: function (res) {
                console.log(res)
              },
            })
            app.globalData.token = '';
            wx.reLaunch({
              url: '/pages/mine/phone-login',
            })
          }
          wx.showToast({
            title: '' + res.data.msg,
          })
        }
      }, fail: function () {

      }, complete: function () {
        wx.hideLoading();
      }
    });
  },
});
